package com.example.human_resource_management.controller;

import com.example.human_resource_management.common.ResponseData;
import com.example.human_resource_management.config.UserContextUtils;
import com.example.human_resource_management.entry.dto.ResourceApplyDto;
import com.example.human_resource_management.entry.po.ResourceApply;
import com.example.human_resource_management.service.ResourceApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/apply")
public class ResourceApplyController {

    @Autowired
    private ResourceApplyService resourceApplyService;

    // 员工查看自己申请资源
    @GetMapping("ziyuan")
    public ResponseData<List<ResourceApplyDto>> selectResourceAppList() {
        Long empId = UserContextUtils.getEmpId();
        if (empId == null) {
            return ResponseData.warn("未获取到员工信息");
        }
        List<ResourceApplyDto> resourceList = resourceApplyService.selectResourceAppListByEmpId(empId);
        return ResponseData.ok(resourceList);
    }

    // 员工申请资源
    @PostMapping("/shenqing")
    public ResponseData<String> createResourceApply(@RequestBody java.util.Map<String, Long> request) {
        Long resId = request.get("resId");
        if (resId == null) {
            return ResponseData.warn("资源ID不能为空");
        }
        return resourceApplyService.createResourceApply(resId);
    }

    // 员工撤销申请
    @PostMapping("/chehui")
    public ResponseData<String> cancelApply(@RequestBody java.util.Map<String, Long> request) {
        Long applyId = request.get("applyId");
        if (applyId == null) {
            return ResponseData.warn("申请ID不能为空");
        }
        return resourceApplyService.cancelApply(applyId);
    }


    //管理员端
    // 管理员端查询所有资源申请
    @GetMapping("/all")
    public ResponseData<List<ResourceApplyDto>> selectAllResourceAppList() {
        List<ResourceApplyDto> resourceList = resourceApplyService.selectAllResourceAppList();
        return ResponseData.ok(resourceList);
    }

    // 管理员端添加资源申请
    @PostMapping("/add")
    public ResponseData<String> insertResourceApply(@RequestBody ResourceApply resourceApply) {
        return resourceApplyService.insertResourceApply(resourceApply);
    }

    // 管理员端更新资源申请
    @PutMapping("/update")
    public ResponseData<String> updateResourceApply(@RequestBody ResourceApply resourceApply) {
        return resourceApplyService.updateResourceApply(resourceApply);
    }

    // 管理员端删除资源申请
    @DeleteMapping("/delete/{applyId}")
    public ResponseData<String> deleteResourceApply(@PathVariable Long applyId) {
        return resourceApplyService.deleteResourceApply(applyId);
    }
}